package elitech.vietnam.myfashion.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.extend.AsyncTask;
import elitech.vietnam.myfashion.utilities.Utilities;
import elitech.vietnam.myfashion.wsclient.Service;

public class ChangePasswordDialog extends Dialog implements android.view.View.OnClickListener {
	
	private MainActivity mContext;
	
	private Button mBtnSave, mBtnCancel;
	private EditText mEtCurrent, mEtNew, mEtConfirm;
	
	public ChangePasswordDialog(MainActivity context) {
		super(context, R.style.Dialog);
		this.mContext = context;
		setContentView(R.layout.changepassword);
		
		mEtCurrent = (EditText) findViewById(R.id.changepass_oldpass);
		mEtNew = (EditText) findViewById(R.id.changepass_newpass1);
		mEtConfirm = (EditText) findViewById(R.id.changepass_newpass2);
		mBtnSave = (Button) findViewById(R.id.newpost_create);
		mBtnCancel = (Button) findViewById(R.id.newpost_cancel);
		mBtnCancel.setOnClickListener(this);
		mBtnSave.setOnClickListener(this);
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}

	@Override
	public void onClick(View v) {
		InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mEtNew.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(mEtCurrent.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(mEtConfirm.getWindowToken(), 0);
		
		switch (v.getId()) {
		case R.id.newpost_create:
			if (mEtCurrent.getText().toString().equals("") || mEtNew.getText().toString().equals("") || mEtConfirm.getText().toString().equals(""))
				Toast.makeText(mContext, mContext.getString(R.string.pleasefillabovefields), Toast.LENGTH_SHORT).show();
			else if (mEtCurrent.getText().toString().equals(mEtNew.getText().toString()))
				Toast.makeText(mContext, mContext.getString(R.string.pleaseenteradifferentpassword), Toast.LENGTH_SHORT).show();
			else if (!Utilities.checkValPass(mEtNew.getText().toString()))
				Toast.makeText(mContext, mContext.getString(R.string.passwordlengthgreaterthanfour), Toast.LENGTH_SHORT).show();
			else if (!mEtNew.getText().toString().equals(mEtConfirm.getText().toString()))
				Toast.makeText(mContext, mContext.getString(R.string.passwordconfirmdoesnotmatch), Toast.LENGTH_SHORT).show();
			else if (!mContext.mUser.Password.equals(Utilities.getMD5(mEtCurrent.getText().toString() + Service.SALT)))
				Toast.makeText(mContext, mContext.getString(R.string.incorrectpassword), Toast.LENGTH_SHORT).show();
			else
				Save();
			break;

		case R.id.newpost_cancel:
			dismiss();
			break;
			
		default:
			break;
		}
		
	}
	
	private void Save() {
		new CallServiceTask() {
			int result = -1;
			public void work() {
				try {
					result = new Service().changePassword(mContext.mUser.Id, mEtNew.getText().toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
			protected void onPostExecute(Void result) {
				if (this.result<=0)
					Toast.makeText(mContext, mContext.getString(R.string.connecterror), Toast.LENGTH_SHORT).show();
				else {
					Toast.makeText(mContext, mContext.getString(R.string.changepasssuccess), Toast.LENGTH_SHORT).show();
					mContext.mUser.Password = Utilities.getMD5(mEtNew.getText().toString() + Service.SALT);
				}
				dismiss();
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
}
